package com.anji.captcha.config;

import com.anji.captcha.properties.AjCaptchaProperties;
import com.anji.captcha.service.CaptchaCacheService;
import com.anji.captcha.service.CaptchaCacheServiceRedisImpl;
import com.anji.captcha.service.impl.CaptchaServiceFactory;
import com.neu.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

@Configuration
@Slf4j
public class CaptchaConfig {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Bean(name = "AjCaptchaCacheService")
    @Primary
    public CaptchaCacheService captchaCacheService(AjCaptchaProperties config){
        //缓存类型redis/local/....
        CaptchaCacheService ret = CaptchaServiceFactory.getCache(config.getCacheType().name());
        log.info("注入***CaptchaCacheService:{}",ret);
        if(ret instanceof CaptchaCacheServiceRedisImpl){
            log.info("注入***CaptchaCacheServiceRedisImpl");
            ((CaptchaCacheServiceRedisImpl)ret).setStringRedisTemplate(stringRedisTemplate);
        }
        return ret;
    }
}